<template>
	<view class="uni_box payment">
		<u-navbar :is-back="false" title="">
			<view class="slot_content">
				<view class="nav_img" @click="backTo">
					<image src="@/static/17.svg"></image>
				</view>
				<view class="nav_title">支付</view>
			</view>
		</u-navbar>
		<view class="info">
			<image :src="cover"></image>
			<view class="info_view">
				<view>
					<text>{{name}}</text>
					<text v-if="number">#{{number}}</text>
				</view>
				<view class="info_price">
					<text>价格：</text>
					<image src="@/static/8.svg"></image>
					<view>{{product_price}}</view>
				</view>
			</view>
		</view>
		<view class="list" v-if="type==40">
			<view class="item">
				<view class="key">购买技术服务费</view>
				<view class="value">¥{{platform_service}}</view>
			</view>
			<view class="item">
				<view class="key">应付金额</view>
				<view class="value">¥{{price}}</view>
			</view>
		</view>
		<view class="payment_type">
			<view class="item" v-for="(item,index) in payList" :class="[active==index?'active':'']"
				@click="changeActive(index)">
				<text>{{item}}</text>
				<image src="@/static/30.svg" v-if="active==index"></image>
				<image src="@/static/29.svg" v-else></image>
			</view>
		</view>

		<view class="payment_btn" v-if="pay_status==0">
			<view class="cancel" @click="cancel">取消</view>
			<view class="confirm" @click="payment">
				<text>确认支付</text>
				<text class="time">{{time}}</text>
			</view>
		</view>
		<view class="cancel_btn" v-else-if="pay_status==2">支付失败</view>

		<u-modal v-model="openShow" width="690" mask-close-able :show-title="false" :show-confirm-button="false">
			<password @submit="submit" />
		</u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: null,
				cover: "",
				name: "",
				number: "",
				product_price: "",

				platform_service: "",
				price: "",
				pay_status: "",
				payList: {},

				active: "",
				pay_password: "",
				openShow: false,

				timer: null,
				time: "",

				type: 0
			}
		},
		onLoad(option) {
			this.id = option.id;
		},
		onUnload() {
			clearTimeout(this.timer);
		},
		onShow() {
			this.orderInfo();
		},
		onHide() {
			clearTimeout(this.timer);
		},
		onReachBottom: function() {},
		methods: {
			async orderInfo() {
				let {
					info,
					order_pay
				} = await this.$u.api.orderInfo({
					id: this.id
				});
				this.cover = info.product_cover;
				this.name = info.product_name;
				this.number = info.product_number;
				this.product_price = info.product.price;

				this.platform_service = info.platform_service;
				this.price = info.price;

				this.end_pay_time = info.end_pay_time;
				this.pay_status = info.pay_status;

				this.payList = order_pay;

				let keys = Object.keys(order_pay);
				this.active = keys.length ? keys[0] : 1;

				this.type = info.type;

				if (this.pay_status == 0) {
					this.handerTime();
				}
			},
			handerTime() {
				let date = this.end_pay_time;
				if (date == 0) return;

				let current = parseInt(new Date().getTime() / 1000);
				if (current >= date) {
					clearTimeout(this.timer);
					this.orderInfo();
					// this.pay_status = 2;
				} else {
					let surplus = date - current;
					let hour = parseInt(surplus / 60 / 60 % 24);
					hour = hour < 10 ? '0' + hour : hour;

					let mm = parseInt(surplus / 60 % 60);
					mm = mm < 10 ? '0' + mm : mm;

					let ss = parseInt(surplus % 60);
					ss = ss < 10 ? '0' + ss : ss;
					this.time = `${hour}:${mm}:${ss}`;

					this.timer = setTimeout(this.handerTime, 1000);
				};
			},
			changeActive(value) {
				this.active = value;
				this.pay_password = "";
			},
			async cancel() {
				await this.$u.api.orderCancel({
					id: this.id
				});
				this.$u.toast("订单取消成功");

				setTimeout(() => {
					uni.navigateBack()
				}, 1000);
			},
			async submit(value) {
				this.pay_password = value;
				this.openShow = false;
				let {
					id
				} = await this.$u.api.orderPayment({
					id: this.id,
					pay_mode: this.active,
					pay_password: this.pay_password
				});
				this.$u.toast("支付成功");

				let url = `/pages/setting/paymentSuccess?id=${id}`;
				setTimeout(() => {
					uni.redirectTo({
						url
					})
				}, 1000);
			},
			async payment() {
				if (this.active == 2) {
					let {
						pay_data
					} = await this.$u.api.orderPayment({
						id: this.id,
						pay_mode: this.active
					});
					this.$store.commit('change_payment_src', pay_data);

					uni.redirectTo({
						url: "/pages/setting/webview"
					})
				}
				if (this.active == 3) {
					this.openShow = true;
				}
			},
			backTo() {
				uni.navigateBack()
			}
		}
	}
</script>

<style scoped lang="scss">
	.payment {
		padding: 44rpx 30rpx;
		box-sizing: border-box;

		.info {
			width: 100%;
			height: 140rpx;
			display: flex;

			image {
				width: 140rpx;
				height: 140rpx;
				border-radius: 10rpx;
				margin-right: 30rpx;
			}

			.info_view {
				flex: 1;
				width: 0;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.info_price {
					height: 32rpx;
					display: flex;
					align-items: center;

					text {
						color: #A7A7A7;
						font-size: 24rpx;
					}

					image {
						width: 20rpx;
						height: 32rpx;
						margin-right: 8rpx;
					}

					view {
						color: #ED905B;
						font-size: 32rpx;
						font-weight: bold;
					}
				}
			}
		}

		.list {
			margin: 44rpx 0;

			.item {
				width: 100%;
				height: 120rpx;
				line-height: 120rpx;
				padding: 0 24rpx;
				box-sizing: border-box;
				display: flex;
				justify-content: space-between;

				.key {
					color: #C2C2C2;
					font-size: 28rpx;
					font-weight: 400;
				}

				.value {
					color: #787878;
					font-size: 28rpx;
				}
			}
		}

		.payment_type {
			width: 100%;
			border-radius: 20rpx;
			margin-top: 52rpx;
			padding: 8rpx;
			box-sizing: border-box;
			background: #F0FBF3;

			.item {
				width: 100%;
				height: 130rpx;
				border-radius: 10rpx;
				padding: 0 24rpx;
				box-sizing: border-box;
				display: flex;
				justify-content: space-between;
				align-items: center;

				text {
					color: #6BD288;
					font-size: 28rpx;
				}

				image {
					width: 36rpx;
					height: 36rpx;
				}

				&.active {
					background: #6BD288;

					text {
						color: #FFFFFF;
					}
				}
			}
		}

		.cancel_btn {
			height: 88rpx;
			border-radius: 20rpx;
			margin-top: 88rpx;
			color: #2A2A2A;
			font-size: 28rpx;
			font-weight: bold;
			border: 1px solid #F0F0F0;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.payment_btn {
			margin-top: 88rpx;
			display: flex;
			justify-content: space-between;

			view {
				width: 330rpx;
				height: 88rpx;
				border-radius: 20rpx;

				&.cancel {
					color: #2A2A2A;
					font-size: 28rpx;
					font-weight: bold;
					border: 1px solid #F0F0F0;
					display: flex;
					justify-content: center;
					align-items: center;
				}

				&.confirm {
					color: #FFFFFF;
					background: #6BD288;
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;

					.time {
						color: #FFFFFF;
						font-size: 20rpx;
						font-weight: 400;
					}
				}
			}
		}
	}
</style>
